package com.ysm.freestyle

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.ysm.freestyle.databinding.ItemScanBinding
import com.ysm.freestyle.viewmodel.ScanResult
import android.text.format.Formatter
import android.util.Log

class ScanResultBindingAdapter(private var dataSet : List<ScanResult>) :
    RecyclerView.Adapter<ScanResultBindingAdapter.ScanViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ScanViewHolder {
        //动态添加布局
        val binding = DataBindingUtil.inflate<ItemScanBinding>(LayoutInflater.from(parent.context),
            R.layout.item_scan,parent,false)
        Log.i("PhotosScan","onCreateViewHolder -> ")
        return ScanViewHolder(binding.root)
    }

    override fun getItemCount(): Int {
        return dataSet.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ScanViewHolder, position: Int) {

        val binding = DataBindingUtil.getBinding<ItemScanBinding>(holder.itemView)
        Log.i("PhotosScan","onBindViewHolder -> ${dataSet.get(position).scanTypeName}")

        if(binding!=null){
            val item = dataSet.get(position)
            binding.itemTitle.text = item.scanTypeName
            binding.itemDesc.text = "${item.scanCount} counts, " +
                    Formatter.formatFileSize(holder.itemView.context, item.scanSize)

        }
    }

    class ScanViewHolder(view: View) : RecyclerView.ViewHolder(view){

    }

    private fun formatSize( context :Context, sizeBytes: Long): String {
        return Formatter.formatFileSize(context, sizeBytes)
    }

}